package com.study.main;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import com.study.customserializable.StringList;

public class Main {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		StringList sList = new StringList();
		sList.add("a");
		sList.add("b");
		sList.add("c");
		sList.add("d");
		sList.add("e");
		sList.add("f");
		sList.add("gh");
		sList.add("i");
		
		System.out.println("sList = " + sList.toString());
		
		writeAndReadObj("test.out", sList);
	}

	private static <T> void writeAndReadObj(final String filename,
			T obj) {
		try {
			ObjectOutputStream o = new ObjectOutputStream(
					new FileOutputStream(filename));
			o.writeObject(obj);
			o.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		try {
			ObjectInputStream in = new ObjectInputStream(
					new FileInputStream(filename));
			
			@SuppressWarnings("unchecked")
			T newobj = (T) in.readObject();// 不需要受检
			System.out.println("newobj = " + newobj);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}
